import {Injectable} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd';
import {ConfigService} from '@kylin/config';
import {HttpService} from '@kylin/http';

@Injectable()
export class ExportService {
    constructor(private message: NzMessageService,
                private config: ConfigService,
                private http: HttpService
    ) {
    }

    onExport(path: string, data) {
        return this.http.get(path, data);
    }

    // 导入excel
    onExportExcel(path, data) {
        this.onExport(path, data)
            .then(res => {
                if (res.status === 200) {
                    this.message.create('success', '导出成功');
                    const eleLink = document.createElement('a');
                    eleLink.setAttribute('target', '_Blank');
                    eleLink.download = '';
                    eleLink.style.display = 'none';
                    eleLink.href = this.config.imgHostname + res.result;
                    document.body.appendChild(eleLink);
                    eleLink.click();
                    document.body.removeChild(eleLink);
                } else {
                    this.message.create('error', res.message);
                }
            })
            .catch(err => {
                this.message.create('error', err);
            });
    }

    onExportWord(path: string, body: any) {
        this.http.post(path, body)
            .then(res => {
                if (res.status === 200) {
                    this.message.create('success', '导出成功');
                    const eleLink = document.createElement('a');
                    eleLink.download = '';
                    eleLink.style.display = 'none';
                    eleLink.href = this.config.imgHostname + res.result;
                    document.body.appendChild(eleLink);
                    eleLink.click();
                    document.body.removeChild(eleLink);
                } else {
                    this.message.create('error', res.message);
                }
            })
            .catch(err => {
                this.message.create('error', err);
            });
    }
}
